<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Common\HttpClient;
use App\Common\Logger;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;

class MiGuPayLogic extends Logic
{

    public static $migu_goods = [
        '110000' => [
            'shopid' => '110000',
            'name' => '300钻石',
            'price' => 300
        ],
        '110001' => [
            'shopid' => '110001',
            'name' => '1000钻石',
            'price' => 1000
        ],
        '110002' => [
            'shopid' => '110002',
            'name' => '2000钻石',
            'price' => 2000
        ],
        '110003' => [
            'shopid' => '110003',
            'name' => '5000钻石',
            'price' => 5000
        ],
        '110004' => [
            'shopid' => '110004',
            'name' => '10000钻石',
            'price' => 10000
        ],
        '110005' => [
            'shopid' => '110005',
            'name' => '20000钻石',
            'price' => 20000
        ],
        '110006' => [
            'shopid' => '110006',
            'name' => '30000钻石',
            'price' => 30000
        ],
    ];

    public function handle($uid,$goods_id,$token,$osid)
    {
        $data = [
            'method' => 'pay_sign',
            'brand' => 8,
            'uid' => DB::table('t_player_info')->where('id',$uid)->value('migu_id'),
            'token' => $token,
            'shopid' => $goods_id,
            'shopcount' => 1,
            'amount' => 100,
            'signtype' => 103,
            'gid' => 111,
            'playid' => 0,
            'cid' => 1005001,
            'osid' => $osid,
            'opid' => 1,
            'scid' => 1,
            'imsi' => '',
            'imei' => '',
            'iccid' => '',
            'pver' => '',
            'plist' => '',
            'vername' => '',
            'pkg' => '',
            'mac' => '',
            'ext' => '',
            'reqtime' => '',
            'format' => 'json',
        ];
        ksort($data);
        $data['sig'] = $this->sign($data);
        $response = (new HttpClient())->get(env('MIGU_PAY_URL') . http_build_query($data));
        $response = json_decode($response,true);
        Logger::instance()->info('请求咪咕生成订单','REQUEST：'.env('MIGU_PAY_URL') . http_build_query($data));
        Logger::instance()->info('响应咪咕生成订单','RESPONSE：'.json_encode($response));
        if (empty($response) || $response['status'] != 0) {
            throw new ApiException('生成咪咕订单失败',6);
        }
        $this->addPayInfo($response['data']['content']['orderno'],$uid,$goods_id);
        return ['orderno' => $response['data']['content']['orderno'],'billingIndex' => $response['data']['content']['billingIndex']];
    }


    private function addPayInfo($orderno,$uid,$goods_id)
    {
        try {
            DB::table('t_pay_info')->insert([
                'pay_type' => 4,
                'order_id' => substr($orderno,0,9),
                'order_status' => 0,
                'uid' => $uid,
                'goods_id' => $goods_id,
                'goods_name' => self::$migu_goods[$goods_id]['name'],
                'price' => self::$migu_goods[$goods_id]['price']
            ]);
        } catch (\Exception $exception) {
            throw new ApiException('生成咪咕订单失败');
        }
    }

    private function sign($data)
    {
        $string = str_replace('&', '', http_build_query($data));
        return md5($string . 'secret=Uw7FXzhyoXzzUq8F0fMZCIcMg1iAyPxE');
    }

}